﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cow.Module;
using System.IO;
using System.CodeDom;

namespace Cow.Handler
{
    /// <summary>
    /// 生成实体对象模型代码
    /// </summary>
    /// 
    public class _ExportEntityHandler : ExportFromCodeDom
    {
        public override string SuffixName
        {
            get { return ".cs"; }
        }

        protected override CodeNamespace CreateCode(Design design)
        {
            CodeNamespace codeNamespace = new CodeNamespace(design.NameSpace);
            // 添加命名空间
            codeNamespace.Imports.Add(new CodeNamespaceImport("System"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.ComponentModel.DataAnnotations"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.Linq"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.Runtime.Serialization"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("System.Text"));
            codeNamespace.Imports.Add(new CodeNamespaceImport("Zoo.Infrastructure"));

            // 创建类
            CodeTypeDeclaration exportClass = new CodeTypeDeclaration(design.ModuleName);
            codeNamespace.Types.Add(exportClass);
            exportClass.BaseTypes.Add("BaseEntity");
            // 添加属性
            foreach (var definition in design.DefinitionInfo)
            {
                //对id字段不做处理
                if (definition.Property.ToLower().Equals("id"))
                {
                    continue;
                }
                DataType dataType = design.DataTypeInfo.FirstOrDefault(item => item.Name.Equals(definition.DataType));
                String type = TypeHelper.GetStandardType(dataType.Csharp, definition.IsNull);
                // 添加字段
                CodeMemberField field = new CodeMemberField();
                field.Attributes = MemberAttributes.Private | MemberAttributes.Final;
                field.Name = definition.Property.ToLower();
                field.Type = new CodeTypeReference(type);
                exportClass.Members.Add(field);
                CodeMemberProperty property = new CodeMemberProperty();
                property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), field.Name)));
                property.SetStatements.Add(new CodeAssignStatement(
                    new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), field.Name),
                    new CodePropertySetValueReferenceExpression()));
                property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
                property.Name = definition.Property;
                property.Comments.Add(new CodeCommentStatement(definition.Label));
                property.Type = new CodeTypeReference(type);
                //添加验证
                if (!definition.IsNull)
                {
                    property.CustomAttributes.Add(new CodeAttributeDeclaration("Required"));
                }
                exportClass.Members.Add(property);
            }
            return codeNamespace;
        }
    }
}
